import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { ApiService } from '../api/api.service';
import { ApiUrl } from '../api/ApiUrl';
import { AlertController } from '@ionic/angular';
import { ArticleVO } from '../vo/ArticleVO';
import { Router } from '@angular/router';
import { LikeVO } from '../vo/LikeVO';
import { UserVO } from '../vo/UserVO';
@Component({
  selector: 'app-mylike',
  templateUrl: './mylike.page.html',
  styleUrls: ['./mylike.page.scss'],
})
export class MylikePage implements OnInit {

  constructor(private router: Router,
    public api: ApiService, public activatedRoute: ActivatedRoute, public alertController: AlertController) { }
  items: LikeVO[] = [];
  userItem: UserVO;
  ngOnInit() {
    this.userItem = ApiUrl.user;
    this.doDownRefresh(null);
  }
   // 下拉刷型界面
   doDownRefresh(evt) {
    if (evt === null) {
      this.onSearch('0', evt);
    } else {
      console.log('iid:' + this.items[0].iid);
      this.onSearch(this.items[0].iid, evt);
    }
  }
  onSearch(iid: string = '0', evt: any, opt: string = 'down') {
    this.api.getItems<LikeVO>(ApiUrl.getMyLike, { 'iid': iid, 'option': opt , 'iuserid' : this.userItem.iid}).subscribe(response => {
      if (evt) {
        evt.target.complete();
      }
      if (response === null || response.length === 0) {
        console.log('没有数据返回');
        return;
      }
      console.log(opt);
      console.log(response);
      if (opt === 'down') { // 下拉刷新，把items追加到新的数据的尾部
        // this.items = this.items.concat(response);
        this.items = response.concat(this.items);
      } else {
        this.items = this.items.concat(response);
        // this.items = this.items.splice(0, 0, response);
      }
    }, () => {
      if (evt) {
        evt.target.complete();
      }
      console.log('出错了');
    });
  }
  doUpRefresh(evt) {
    console.log('上拉刷新');
    this.onSearch(this.items[this.items.length - 1].iid, evt, 'up');
  }

  goToDetails(iid, itype) {
    if (itype === 1) {
      this.router.navigate(['/article', {'iid': iid}]);
    } else {
      this.router.navigate(['/video', {'iid': iid}]);
    }
  }
}
